package com.zhiyume.service;

import com.zhiyume.dao.api.TSplashMapper;
import com.zhiyume.dao.entity.TAdminSys;
import com.zhiyume.dao.entity.TSplash;
import com.zhiyume.dao.entity.TSplashCriteria;
import com.zhiyume.service.util.DateUtil;
import com.zhiyume.service.util.GenerateKeyUtil;
import com.zhiyume.service.util.StringUtil;
import com.zhiyume.shiro.AdminSecurityManager;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class SplashService {
    @Autowired
    TSplashMapper tSplashMapper;

    public void addSplash(TSplash tSplash){
        TAdminSys sysUser = AdminSecurityManager.getSessionTAdminSys();
        long id = GenerateKeyUtil.generationPK();
        tSplash.setId(id);
        tSplash.setCreateTime(DateUtil.getNow());
        tSplash.setCreateId(sysUser.getId());
        tSplashMapper.insert(tSplash);
    }

    public List<TSplash> getSplashList(TSplash tSplash){
        TSplashCriteria t = new TSplashCriteria();
        TSplashCriteria.Criteria c = t.createCriteria();
        if(tSplash.getId()!=null){
            c.andIdEqualTo(tSplash.getId());
            return tSplashMapper.selectByExample(t);
        }
        if(tSplash.getCreateId()!=null){
            c.andCreateIdEqualTo(tSplash.getCreateId());
        }
        if(StringUtil.isNotBlank(tSplash.getType())){
            c.andTypeEqualTo(tSplash.getType());
        }
        t.setOrderByClause("id desc,`s_id` desc");
        t.setLimitStart(0);
        t.setLimitEnd(10);
        return tSplashMapper.selectByExample(t);
    }


    public TSplash getLastSplash(TSplash tSplash){
        TSplashCriteria t = new TSplashCriteria();
        TSplashCriteria.Criteria c = t.createCriteria();
        if(tSplash.getId()!=null){
            c.andIdEqualTo(tSplash.getId());
            return tSplashMapper.selectByExampleWithBLOBs(t).get(0);
        }
        if(tSplash.getCreateId()!=null){
            c.andCreateIdEqualTo(tSplash.getCreateId());
        }
        if(StringUtil.isNotBlank(tSplash.getType())){
            c.andTypeEqualTo(tSplash.getType());
        }
        t.setOrderByClause("id desc,`s_id` desc");
        t.setLimitStart(0);
        t.setLimitEnd(1);
        List<TSplash> list = tSplashMapper.selectByExampleWithBLOBs(t);
        if(CollectionUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return new TSplash();
    }

}
